package com.borrowed.book.book.controller;

import com.borrowed.book.book.domain.*;
import com.borrowed.book.membercard.entity.SMemberCard;
import com.borrowed.book.membercard.service.SMemberCardService;
import com.borrowed.book.system.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "会员卡管理")
@RequestMapping("/api/memCard")
public class MemCardController {
    @Autowired
    private SMemberCardService memCardService;


    @ApiOperation(value = "添加会员卡",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult  AddMemCard(@RequestBody SMemberCard memberCard){

        AjaxResult result;
        try{
            memCardService.addMemCard(memberCard);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "会员卡一览",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public AjaxResult selectMemberCard(){
        AjaxResult result;
        try {
            List<SMemberCard> list = memCardService.selectMemberCards();
            return AjaxResult.sucess("查询成功", list);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "更新",httpMethod = "PUT")
    @PutMapping("/update")
    @ResponseBody
    @Transactional
    public AjaxResult updateMemberCard(@RequestBody SMemberCard address){
        AjaxResult result;
        try{
            memCardService.updateMemCard(address);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除",httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @Transactional
    public AjaxResult deleteMemberCard(@RequestParam(value = "id",required = true ) Long id){
        AjaxResult result;
        try{
            memCardService.deleteMemCard(id);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }



    @ApiOperation(value = "当前登录人绑定会员卡",httpMethod = "POST")
    @PostMapping("/bindingCard")
    @ApiImplicitParam(name="cardId",value = "会员卡ID",type = "Long",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult  bindingMembercard(Long  cardId){
        AjaxResult result;
        try{
            memCardService.bindingMemberCard(cardId,null);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

}
